import type { Request, Response } from 'express';

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

// 当前用户的权限，如果为空代表没登录
let currentUser: API.UserWithRoles | null = null;

const getCurrentUser = () => {
  return currentUser;
};

// 模拟权限数据
const mockPermissions: API.Permission[] = [
  {
    id: '1',
    name: '仪表盘',
    code: 'dashboard:view',
    type: 'menu',
    resource: '/dashboard',
    action: 'view',
    description: '查看仪表盘页面',
    sort: 1,
    status: 'active',
  },
  {
    id: '5',
    name: '系统设置',
    code: 'setting:view',
    type: 'menu',
    resource: '/settings',
    action: 'view',
    description: '查看系统设置页面',
    sort: 5,
    status: 'active',
  },
];

// 模拟角色数据
const mockRoles: API.Role[] = [
  {
    id: 'role-1',
    name: '超级管理员',
    code: 'admin',
    description: '拥有系统所有权限',
    status: 'active',
    permissions: mockPermissions,
    createdAt: '2024-01-01T00:00:00Z',
    updatedAt: '2024-01-01T00:00:00Z',
  },
  {
    id: 'role-2',
    name: '普通用户',
    code: 'user',
    description: '基础查看权限',
    status: 'active',
    permissions: mockPermissions.filter(p => 
      p.code === 'dashboard:view' || p.code === 'setting:view'
    ),
    createdAt: '2024-01-01T00:00:00Z',
    updatedAt: '2024-01-01T00:00:00Z',
  },
];

export default {
  // 登录接口
  'POST /api/login/account': async (req: Request, res: Response) => {
    const { password, username, type } = req.body;
    console.log('收到登录请求:', { username, password, type });
    
    await waitTime(500);
    
    if (password === 'ant.design' && username === 'admin') {
      currentUser = {
        userid: '00000001',
        name: '管理员',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        email: 'admin@example.com',
        signature: '海纳百川，有容乃大',
        title: '系统管理员',
        group: '技术部',
        access: 'admin',
        roles: [mockRoles[0]], // 超级管理员角色
        permissions: mockPermissions, // 拥有所有权限
      };
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'admin',
      });
      return;
    }
    
    if (password === 'ant.design' && username === 'user') {
      currentUser = {
        userid: '00000002',
        name: '普通用户',
        avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
        email: 'user@example.com',
        signature: '好好学习，天天向上',
        title: '普通员工',
        group: '业务部',
        access: 'user',
        roles: [mockRoles[1]], // 普通用户角色
        permissions: mockPermissions.filter(p => 
          p.code === 'dashboard:view' || p.code === 'setting:view'
        ), // 只有基础权限
      };
      res.send({
        status: 'ok',
        type,
        currentAuthority: 'user',
      });
      return;
    }

    res.send({
      status: 'error',
      type,
      currentAuthority: 'guest',
    });
  },

  // 退出登录
  'POST /api/login/outLogin': (_req: Request, res: Response) => {
    currentUser = null;
    res.send({ data: {}, success: true });
  },

  // 获取当前用户信息
  'GET /api/currentUser': (_req: Request, res: Response) => {
    const user = getCurrentUser();
    if (!user) {
      res.status(401).send({
        data: {
          isLogin: false,
        },
        errorCode: '401',
        errorMessage: '请先登录！',
        success: true,
      });
      return;
    }
    
    res.send({
      success: true,
      data: user,
    });
  },
};
